package org.bigdata.framework.core.protocol;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.bigdata.framework.core.model.BaikeEntry;
import org.bigdata.framework.core.model.Classes;
import org.bigdata.framework.core.model.ShardingRepositoryCount;

/**
 * 知识库动态表接口服务
 * @author anhuifeng
 *
 */
public interface IDynamicService {
	
	/* 分库分表相关 start */
	
	// @构建分库分表-相关数据表
	void buildShardingRepositoryTable(String classesEn, String repositoryDataSourceId);
	// @删除分库分表-相关数据表（*数据表存储数据超过1万以上慎用此方法，应该先清空数据再执行此方法）
	void deleteShardingRepositoryTable(String classesEn, String repositoryDataSourceId); 
	// @新增动态数据(单个)-用于数据处理-自动分配数据
	String addDynamicData(String classesEn, Map<String,Object> dynamicData,String createTime,String createUserId, String status, String version,String classesId, String dataSource, boolean sharding);
	// @新增动态数据(批量)-用于批量导入数据-自动分配数据
	boolean addDynamicData(String classesEn, List<Map<String,Object>> dynamicDatas,String createTime,String createUserId, String status, String version,String classesId, String dataSource, boolean sharding);
	
	//分库分表相关方法统一增加-指定库和表
	// @新增动态数据(单个)
	String addDynamicData(String classesEn, Map<String,Object> dynamicData,String createTime,String createUserId, String status, String version,String classesId, String dataSource, String shardingDataSourceId, String tableName); 
	// @更新动态数据(dynamicData中包含id)
	void editDynamicData(String classesEn, Map<String,Object> dynamicData,String updateTime, String updateUserId, String status, String version, String id, String shardingDataSourceId, String tableName);
	// @按条件更新多个字段集合
	boolean editDynamicData(String classesEn, Map<String,Object> dynamicData, Map<String,Object> conditions,String updateTime,String updateUserId, String status, String version, String shardingDataSourceId, String tableName);
	// @按ids集合更新多个字段集合
	void editDynamicData(String classesEn, List<Map<String,Object>> dynamicDatas, String updateTime, String updateUserId, String status, String version, String shardingDataSourceId, String tableName);
	// @更新数据创建人
	void editDynamicDataCreateUser(String classesEn, String id, String userId, String shardingDataSourceId, String tableName);
	// @删除动态数据（单个） (dynamicData中包含id)
	void deleteDynamicData(String classesEn, Map<String,Object> dynamicData, String shardingDataSourceId, String tableName);
	// @查询动态数据(单个)
	Map<String,Object> findDynamicData(String classesEn, String id, String shardingDataSourceId, String tableName);
	// @查询动态数组（单个+指定字段）
	Map<String,Object> findDynamicData(String classesEn, String id,String fields, String shardingDataSourceId, String tableName);
	//分页
	List<Map<String, Object>> getDynamicData(String classesEn, Map<String,Object> conditions, int pageNumber, int pageSize, String shardingDataSourceId, String tableName);
	List<Map<String, Object>> getDynamicDataList(String classesEn,String showField, Map<String,Object> conditions, int pageNumber, int pageSize, String shardingDataSourceId, String tableName);
	List<Map<String, Object>> getDynamicDataList(String classesEn,String showField, Map<String, Object> conditions, String orderByField, String orderByValue, int pageNumber, int pageSize, String shardingDataSourceId, String tableName);
	List<Map<String, Object>> getDynamicData(String classesEn, Map<String, Object> conditions, String orderByField, String orderByValue, int pageNumber, int pageSize, String shardingDataSourceId, String tableName);
	List<Map<String, Object>> getDynamicData(String classesEn, String id, int pageSize, String shardingDataSourceId, String tableName);
	Long getDynamicCount(String classesEn, Map<String,Object> conditions, String shardingDataSourceId, String tableName);
	Long getDynamicCount(String classesEn, String createUserId, String startTime, String endTime, String shardingDataSourceId, String tableName);
		
	// 关联属性管理
	//分库-分库
	void saveDynamicObjectDataST(String classesEn, String id,String relevanceClassesEn, String relevanceId, Integer objectPropertyId, String createTime, String createUserId, String sourceShardingDataSourceId,String sourceTableName, String targetShardingDataSourceId,String targetTableName);
	//不分库-分库
	void saveDynamicObjectDataT(String classesEn, String id,String relevanceClassesEn, String relevanceId, Integer objectPropertyId, String createTime, String createUserId, String targetShardingDataSourceId, String targetTableName);
	//分库-不分库
	void saveDynamicObjectDataS(String classesEn, String id,String relevanceClassesEn, String relevanceId, Integer objectPropertyId, String createTime, String createUserId, String sourceShardingDataSourceId, String sourceTableName);
	void deleteDynamicObjectData(String classesEn, String id, Integer objectPropertyId, String shardingDataSourceId, String tableName);
	public Long getDynamicObjectDataCount(String classesEn, String sourceId, String targetId, Integer objectPropertyId, String shardingDataSourceId, String tableName);
	List<Map<String,Object>> getDynamicObjectData(String classesEn, String sourceId, String targetId, Integer objectPropertyId, int pageNumber, int pageSize, String shardingDataSourceId, String tableName);
	
	//数据发布相关接口
	List<SolrInputDocument> getIndexData(String classesEn,List<Map<String, Object>> list, String shardingDataSourceId, String tableName);
	void submitDynamicData(String classesEn, String submitTime, String submitUserId, String shardingDataSourceId, String tableName);
	void submitDynamicData(String classesEn, String submitTime, String submitUserId, String id, String shardingDataSourceId, String tableName);
	void submitCommitIndex(String classesEn,List<Map<String,Object>> list, String shardingDataSourceId, String tableName);
	
	/*分库分表相关 end*/

	/* 知识管理 start */
	
	/* 状态字段定义 status：0-加工中 1-审核通过 -1-审核不通过 2-已发布 */
	//重新构建动态数据库表
	void editClassesDb(String classesEn);
	// 新增动态数据(单个)
	String addDynamicData(String classesEn, Map<String,Object> dynamicData,String createTime,String createUserId, String status, String version,String classesId, String dataSource);
	// 新增动态数据(批量)
	boolean addDynamicData(String classesEn, List<Map<String,Object>> dynamicDatas,String createTime,String createUserId, String status, String version,String classesId, String dataSource);
	// 更新动态数据(dynamicData中包含id)
	void editDynamicData(String classesEn, Map<String,Object> dynamicData,String updateTime, String updateUserId, String status, String version, String id);
	// 按条件更新多个字段集合
	boolean editDynamicData(String classesEn, Map<String,Object> dynamicData, Map<String,Object> conditions,String updateTime,String updateUserId, String status, String version);
	// 按ids集合更新多个字段集合
	void editDynamicData(String classesEn, List<Map<String,Object>> dynamicDatas, String updateTime, String updateUserId, String status, String version);
	// 更新数据创建人
	void editDynamicDataCreateUser(String classesEn, String id, String userId);
	// 删除动态数据（单个） (dynamicData中包含id)
	void deleteDynamicData(String classesEn, Map<String,Object> dynamicData);
	// 查询动态数据(单个)
	Map<String,Object> findDynamicData(String classesEn, String id);
	// 查询动态数组（单个+指定字段）
	Map<String,Object> findDynamicData(String classesEn, String id,String fields);
	
	// 新增版本记录（mongodb）
	void addDynamicDataVersion(String classesEn, Map<String,Object> dynamicData,String updateTime,String updateUserId, String status, String version,String classesId, String dataSource, String sourceDataId);
	// 查询所有版本记录（mongodb）
	List<Map<String, Object>> getDynamicDataVersion(String classesEn, String sourceDataId, Map<String,Object> conditions, int pageNumber, int pageSize);
	// 查询所有版本记录总数（mongodb）
	Long getDynamicDataVersionCount(String classesEn, String sourceDataId, Map<String,Object> conditions);
	// 查询单个记录（mongodb）
	Map<String,Object> findDynamicDataVersion(String classesEn, String id);
	
	// 查询动态数据(列表+分页)
	List<Map<String, Object>> getDynamicData(String classesEn, Map<String,Object> conditions, int pageNumber, int pageSize);
	List<Map<String, Object>> getDynamicDataList(String classesEn,String showField, Map<String,Object> conditions, int pageNumber, int pageSize);
	List<Map<String, Object>> getDynamicDataList(String classesEn,String showField, Map<String, Object> conditions, String orderByField, String orderByValue, int pageNumber, int pageSize);
	List<Map<String, Object>> getDynamicData(String classesEn, Map<String, Object> conditions, String orderByField, String orderByValue, int pageNumber, int pageSize);
	List<Map<String, Object>> getDynamicData(String classesEn, String id, int pageSize);
	Long getDynamicCount(String classesEn, Map<String,Object> conditions);
	Long getDynamicCount(String classesEn, String createUserId, String startTime, String endTime);
	
	/* 知识管理 end */
	
	/* 关联属性管理 start */
	void saveDynamicObjectData(String classesEn, String id,String relevanceClassesEn, String relevanceId, Integer objectPropertyId, String createTime, String createUserId);
	void deleteDynamicObjectData(String classesEn, String id, Integer objectPropertyId);
	public Long getDynamicObjectDataCount(String classesEn, String sourceId, String targetId, Integer objectPropertyId);
	List<Map<String,Object>> getDynamicObjectData(String classesEn, String sourceId, String targetId, Integer objectPropertyId, int pageNumber, int pageSize);
	/* 关联属性管理 end */
	
	// 发布数据（按知识库发布数据）
	// 数据列表封装成solr对象
	// 单个类下批量发布
	List<SolrInputDocument> getIndexData(String classesEn,List<Map<String, Object>> list);
	// 单库发布
	void submitDynamicData(String classesEn, String submitTime, String submitUserId);
	// 单条数据发布
	void submitDynamicData(String classesEn, String submitTime, String submitUserId, String id);
	
	/* 内存缓存对象 start */
	// 类列表：key=领域en.classesList
	// dp列表：key=领域en.类en.dpList
	// op列表：key=领域en.类en.opList
	// 具体属性：key=领域en.类en.数据属性en或者对象属性en
	/* 缓存数据结构至内存  */
	void saveObjectForCache();
	/* 内存缓存对象 end */
	/* 获得缓存的数据 */
	HashMap<String,Object> getDataSchema();
	/* 索引管理 start */
	
	/* 批量发布关键词索引 */
	void submitKwIndex(List<SolrInputDocument> list);
	/* 批量发布词表索引 */
	void submitWordIndex(List list, String type);
	/* 批量发布百科索引 */
	void submitBaikeIndex(List<BaikeEntry> list);
	/* 批量发布索引-hymax*/
	void submitIndex(List<SolrInputDocument> list);
	void submitCommitIndex(String classesEn,List<Map<String,Object>> list);
	/* 删除类索引 */
	void deleteIndexByClassesEn(String classesEn);
	/* 按表达式删除类索引 */
	void deleteIndexByCondition(String queryStr);
	/* 删除所有索引 */
	void deleteAllIndex();
	/* 删除索引-ids */
	void deleteIndex(String[] ids);
	/* 手动发布索引-hymax */
	void commitIndex();
	/* 手动发布索引-关键词core */
	void commitKwIndex();
	/* 手动发布索引-词表core */
	void commitWordIndex();
	/* 手动发布索引-百科core */
	void commitBaikeIndex();
	/* 知识百科-按表达式删除索引 */
	void deleteBaikeIndexByCondition(String queryStr);
	/* 知识百科-删除所有索引 */
	void deleteAllBaikeIndex();
	/* 知识词表-按表达式删除索引 */
	void deleteWordIndexByCondition(String queryStr);
	/* 知识词表-删除所有索引 */
	void deleteAllWordIndex();
	/* 关键词库-按表达式删除索引 */
	void deleteKwIndexByCondition(String queryStr);
	/* 关键词库-删除所有索引 */
	void deleteAllKwIndex();
	/* 自动标引库-按表达式删除索引 */
	void deleteIndexWeightIndexByCondition(String queryStr);
	/* 自动标引库-删除所有索引 */
	void deleteAllIndexWeightIndex();
	
	/* 单调发布百科词条 */
	void submitBaikeIndex(BaikeEntry baikeEntry);
	/* 批量构建百科词条Document */
	List<SolrInputDocument> buildBaikeEntry(List<BaikeEntry> list);
	
	/* 发布叙词表索引 */
	void submitThesauriWordIndex(String thesauriWordId);
	/* 发布普通词表索引 */
	void submitGeneralWordIndex(String generalWordId);
	/* 索引管理 end */
	
	
	/* 分布式任务调度 start */
	/* 获得所有子系统 */
	List<HashMap<String,String>> getSystemList();
	/* 获得所有子系统模块 */
	List<HashMap<String,String>> getModuleList(String systemEn);
	/* 获得所有子系统模块 hashMap主要包括任务名称、任务类型和任务id */
	List<HashMap<String,Object>> getTaskList(String systemEn, String moduleEn);
	
	/* 分布式任务调度 end */
	 
	/*   标引 api solr发布  start*/
	/* 单调发布标引词条 */
	void submitIndexingIndex(long id,String title,String keyword,String summary,String text,String extend1,String extend2,String extend3);
	/* 批量标引 */
	void submitIndexingIndex(List<Map<String, String>> list);
	/*   标引 api solr发布  end*/
	void deleteAllIndexingWeight();
	
	/*转化成sql语句，批量插入：知识库主表-对象属性知识库-知识库从表----主表和op表在一个数据源，从表可能在别的数据源*/
	Map<String,List<String>> getDynamicDataSqls(Classes sourceClassesEn,Classes targetClassesEn,String sourceTableName,String targetTableName,String opTableName,Map<String, Object> sourceData,List<Map<String, Object>>targetData,String sourceShardingDataSourceId,String targetShardingDataSourceId);
	Map<String,String> getDynamicMutilValueSqls(String sourceClassesEn,String targetClassesEn,String sourceTableName,String targetTableName,String opTableName,Map<String, Object> sourceData,List<Map<String, Object>>targetData,String sourceShardingDataSourceId,String targetShardingDataSourceId);
	
	/*批量插入：知识库主表-对象属性知识库-知识库从表*/
	void batchSubmitDynamicDataSqls(String sourceClassesEn,String targetClassesEn,List<Map<String, Object>>sourceDataList,Map<String,List<Map<String,Object>>>targetDataList);

	
	/*发布带附件的知识库*/
	List<ModifiableSolrParams> getIndexDataWithAttachment(String classesEn,List<Map<String, Object>> list, String shardingDataSourceId, String tableName);
	void submitDynamicDataWithAttachment(String classesEn, String submitTime, String submitUserId, String shardingDataSourceId, String tableName);
	void submitDynamicDataWithAttachment(String classesEn, String submitTime, String submitUserId, String id, String shardingDataSourceId, String tableName);
	void submitCommitIndexWithAttachment(String classesEn,List<Map<String,Object>> list, String shardingDataSourceId, String tableName);
}
